<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/4/26
 * Time: 14:45
 */

namespace App\Rep;


use App\Models\Employee;
use App\Models\OrderChild;
use Illuminate\Support\Facades\DB;

class SalaryRep
{
    //员工薪资列表
    public function getSalaryListRep($params){
        //获取普通员工列表
        $list = OrderChild::Search($params)
            ->where('order_child.is_del',2)
            ->whereNotNull('order_child.employee_user_id')
            ->whereNotNull('order_child.submit_time')
            ->with(['employee','job_type'])
            ->leftjoin('order', 'order.id', '=', 'order_child.order_id')
            ->select(
                'order_child.employee_user_id','order_child.job_type_id','order_child.price',
                'order.num',
                DB::raw('sum(likai_order_child.price * likai_order.num) as total_sales'),
                DB::raw('count(likai_order.num) as order_count'),
                DB::raw('sum(likai_order.num) as sum_num')

            )
            ->groupBy('employee_user_id')
            ->paginate($params['per_page']??30);
        return $list;

    }

    /**
     * 获取员工薪资详情
     * @param $params
     */
    public function getSalaryInfoRep($params){
        $list = OrderChild::Search($params)
            ->with(['order'=>function($q){
                $q->with(['color'=>function($q){
                    return $q->select('id','color');
                },'product'=>function($q){
                    return $q->select('id','product_name');
                },'customer'=>function($q){
                    return $q->select('id','customer_name','customer_address');
                },'modelNumber'=>function($q){
                    return $q->select('id','model_number');
                },'specification'=>function($q){
                    return $q->select('id','specification');
                }]);

            },
                'employee'=>function($q){
                $q->select('user_id','employee_name','base_pay','id');
                }
            ])
            ->where('is_del',2)
            ->whereNotNull('submit_time')
            ->paginate($params['per_page']??10);
        foreach($list as $k=>$v){
            $list[$k]->total_sales = $v->order->num * $v->price;
        }
        return $list;
    }

    /**
     * 修改员工工资
     * @param $params
     */
    public function changeSalaryRep($params){
        $order_child_id = $params['order_child_id'];
        $model = OrderChild::find($order_child_id);
        $model->price = $params['price'];
        $model->save();
        return $model;
    }



}
